﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Drawing2D;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint;

namespace Moss.Workflow.Activities
{
    public class MossTaskManagerActivityDesigner : SequenceDesigner
    {
    }

    [Designer(typeof(MossTaskManagerActivityDesigner), typeof(IDesigner))]
    [ToolboxItem(typeof(ActivityToolboxItem))]
    public partial class MossTaskManagerActivity : SequenceActivity
    {
        #region CTOR
        public MossTaskManagerActivity()
        {
            InitializeComponent();
        }
        #endregion

        #region Initialization Bound Properties Promoted
        #region WorkflowToken Property
        public static DependencyProperty WorkflowTokenProperty = DependencyProperty.Register("WorkflowToken", typeof(CorrelationToken), typeof(MossTaskManagerActivity));
        [DescriptionAttribute("WorkflowToken")]
        [CategoryAttribute("Properties")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        [DefaultValue((string)null), TypeConverter("System.Workflow.Activities.CorrelationTokenTypeConverter, System.Workflow.Activities, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")]
        public CorrelationToken WorkflowToken
        {
            get
            {
                return ((CorrelationToken)(base.GetValue(WorkflowTokenProperty)));
            }
            set
            {
                base.SetValue(WorkflowTokenProperty, value);
                SendWFTaskEmail.CorrelationToken = value;
            }
        }
        #endregion
        #region InstanceData Property (SPWorkflowTaskProperties)
        public static DependencyProperty InstanceDataProperty = DependencyProperty.Register("InstanceData", typeof(SPWorkflowTaskProperties), typeof(MossTaskManagerActivity));
        [DescriptionAttribute("InstanceData")]
        [CategoryAttribute("Properties")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public SPWorkflowTaskProperties InstanceData
        {
            get
            {
                return ((SPWorkflowTaskProperties)(base.GetValue(MossTaskManagerActivity.InstanceDataProperty)));
            }
            set
            {
                base.SetValue(MossTaskManagerActivity.InstanceDataProperty, value);
            }
        }
        #endregion
        #region WorkflowProperties Property
        public static DependencyProperty WorkflowPropertiesProperty = DependencyProperty.Register("WorkflowProperties", typeof(SPWorkflowActivationProperties), typeof(MossTaskManagerActivity));
        [DescriptionAttribute("WorkflowProperties")]
        [CategoryAttribute("Properties")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public SPWorkflowActivationProperties WorkflowProperties
        {
            get
            {
                return ((SPWorkflowActivationProperties)(base.GetValue(MossTaskManagerActivity.WorkflowPropertiesProperty)));
            }
            set
            {
                base.SetValue(MossTaskManagerActivity.WorkflowPropertiesProperty, value);
            }
        }
        #endregion
        #endregion

        #region Task Bound Properties Promoted
        #region TaskContentTypeId
        public static DependencyProperty TaskContentTypeIdProperty = DependencyProperty.Register("TaskContentTypeId", typeof(System.String), typeof(MossTaskManagerActivity));
        [DescriptionAttribute("TaskContentTypeId")]
        [CategoryAttribute("Properties")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public string TaskContentTypeId
        {
            get
            {
                return ((string)(base.GetValue(MossTaskManagerActivity.TaskContentTypeIdProperty)));
            }
            set
            {
                base.SetValue(MossTaskManagerActivity.TaskContentTypeIdProperty, value);
            }
        }
        #endregion
        #endregion

        #region Task Bound Events Promoted
        #region IsTaskCompleted Event
        public static DependencyProperty IsTaskCompletedEvent = DependencyProperty.Register("IsTaskCompleted", typeof(EventHandler), typeof(MossTaskManagerActivity));
        [Description("Event handler invoked when the task is changed. User can return whether or not the task is complete in the event arguments.")]
        [CategoryAttribute("Events")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public event EventHandler IsTaskCompleted
        {
            add
            {
                base.AddHandler(IsTaskCompletedEvent, value);
            }
            remove
            {
                base.RemoveHandler(IsTaskCompletedEvent, value);
            }
        }
        #endregion
        #region OnTaskDeleted Event
        public static DependencyProperty OnTaskDeletedEvent = DependencyProperty.Register("OnTaskDeleted", typeof(EventHandler), typeof(MossTaskManagerActivity));
        [DescriptionAttribute("OnTaskDeleted")]
        [CategoryAttribute("Events")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public event EventHandler OnTaskDeleted
        {
            add
            {
                base.AddHandler(OnTaskDeletedEvent, value);
            }
            remove
            {
                base.RemoveHandler(OnTaskDeletedEvent, value);
            }
        }
        #endregion
        #region OnSendNotification Event
        public static DependencyProperty OnSendNotificationEvent = DependencyProperty.Register("OnSendNotification", typeof(EventHandler), typeof(MossTaskManagerActivity));

        [DescriptionAttribute("OnSendNotification")]
        [CategoryAttribute("Events")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public event EventHandler OnSendNotification
        {
            add
            {
                base.AddHandler(MossTaskManagerActivity.OnSendNotificationEvent, value);
            }
            remove
            {
                base.RemoveHandler(MossTaskManagerActivity.OnSendNotificationEvent, value);
            }
        }
        #endregion
        #endregion

        #region Task related variables
        private bool taskCompleted = false;
        private bool taskDeleted = false;
        public String taskOutcome = default(System.String);
        public Guid TaskId = default(System.Guid);
        public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties TaskProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties AfterProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties BeforeProperties = new Microsoft.SharePoint.Workflow.SPWorkflowTaskProperties();
        public String logToHistory_TaskCompleted_HistoryDescription = default(System.String);
        #endregion

        #region Activity Handlers
        private void CreateTaskHandler(object sender, EventArgs e)
        {
            CreateTaskWithContentType task = sender as CreateTaskWithContentType;
            TaskId = Guid.NewGuid();
        }
        private void AdditionalNotificationRequired(object sender, ConditionalEventArgs e)
        {
            e.Result = !string.IsNullOrEmpty(TaskProperties.ExtendedProperties["Notification"].ToString());
        }
        private void SendTaskEmailHandler(object sender, EventArgs e)
        {
            TaskProperties.TaskItemId = CreateWFTask.ListItemId;
            SendWFTaskEmail.To = TaskProperties.ExtendedProperties["Notification"].ToString();
            NotificationEventArgs args = new NotificationEventArgs(TaskProperties);
            base.RaiseEvent(OnSendNotificationEvent, this, args);
            SendWFTaskEmail.Body = args.Body;
        }
        private void TaskNotCompleted(object sender, ConditionalEventArgs e)
        {
            e.Result = !this.taskCompleted;
        }
        private void OnTaskChangedHandler(object sender, ExternalDataEventArgs e)
        {
            TaskEventArgs args = new TaskEventArgs(this.BeforeProperties, this.AfterProperties);
            base.RaiseEvent(IsTaskCompletedEvent, this, args);
            this.taskCompleted = args.Result;
            logToHistory_TaskCompleted_HistoryDescription = string.Format("{0} verification was done by {1}", TaskProperties.ExtendedProperties["Area"].ToString(), WorkflowProperties.Web.EnsureUser(e.Identity).Name);
        }
        private void OnTaskDeletedHandler(object sender, ExternalDataEventArgs e)
        {
            TaskEventArgs args = new TaskEventArgs(null, this.AfterProperties);
            base.RaiseEvent(OnTaskDeletedEvent, this, args);
            this.taskDeleted = true;
            this.taskCompleted = true;
            logToHistory_TaskCompleted_HistoryDescription = string.Format("{0} verification was deleted by {1}", TaskProperties.ExtendedProperties["Area"].ToString(), WorkflowProperties.Web.EnsureUser(e.Identity).Name);
        }
        private void TaskNotDeleted(object sender, ConditionalEventArgs e)
        {
            e.Result = !this.taskDeleted;
        }
        private void LogToHistory_SendWFTaskEmail_Handler(object sender, EventArgs e)
        {
            (sender as LogToHistoryListActivity).HistoryDescription = "Sent notification to " + TaskProperties.ExtendedProperties["Notification"].ToString();
            (sender as LogToHistoryListActivity).HistoryOutcome = "Notification complete";
        }
        #endregion
    }
}
